<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>Java集成Ffmpeg实现视频转M3U8 | 飞の博客</title><meta name="author" content="Fei"><meta name="copyright" content="Fei"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="Java集成Ffmpeg实现视频转M3U8">
<meta property="og:type" content="article">
<meta property="og:title" content="Java集成Ffmpeg实现视频转M3U8">
<meta property="og:url" content="https://tengfei2233.gitee.io/2023/3/2/Java%E8%B0%83%E7%94%A8Ffmpeg%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E8%BD%ACM3U8/index.html">
<meta property="og:site_name" content="飞の博客">
<meta property="og:description" content="Java集成Ffmpeg实现视频转M3U8">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-3-2/ffmpeg.jpg">
<meta property="article:published_time" content="2023-03-02T03:27:39.000Z">
<meta property="article:modified_time" content="2023-03-02T03:27:39.000Z">
<meta property="article:author" content="Fei">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Ffmpeg">
<meta property="article:tag" content="视频转码">
<meta property="article:tag" content="M3U8">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-3-2/ffmpeg.jpg"><link rel="shortcut icon" href="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/icon.png"><link rel="canonical" href="https://tengfei2233.gitee.io/2023/3/2/Java%E8%B0%83%E7%94%A8Ffmpeg%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E8%BD%ACM3U8/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="msvalidate.01" content="2ADFA30867CBC6E090E3C5B244DC311D"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.3.0/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.staticfile.org/fancyapps-ui/4.0.31/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?07a1f58fb95d3d4a97c2ae0a181c9964";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.staticfile.org/flickr-justified-gallery/2.1.2/fjGallery.min.js',
      css: 'https://cdn.staticfile.org/flickr-justified-gallery/2.1.2/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Java集成Ffmpeg实现视频转M3U8',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-03-02 11:27:39'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })

if (false) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/fei.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">24</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://github.com/tengfei2233"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-3-2/ffmpeg.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="飞の博客"><span class="site-name">飞の博客</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://github.com/tengfei2233"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Java集成Ffmpeg实现视频转M3U8</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-03-02T03:27:39.000Z" title="发表于 2023-03-02 11:27:39">2023-03-02</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-03-02T03:27:39.000Z" title="更新于 2023-03-02 11:27:39">2023-03-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Java/">Java</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Java/SpringBoot/">SpringBoot</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>8分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Java集成Ffmpeg实现视频转M3U8"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="Java集成Ffmpeg实现视频转M3U8"><a href="#Java集成Ffmpeg实现视频转M3U8" class="headerlink" title="Java集成Ffmpeg实现视频转M3U8"></a>Java集成Ffmpeg实现视频转M3U8</h2><h3 id="Ffmpeg是什么"><a href="#Ffmpeg是什么" class="headerlink" title="Ffmpeg是什么"></a>Ffmpeg是什么</h3><blockquote>
<p>FFmpeg 强大专用于处理音视频的开源库，包含了先进的音视频编解码库，提供了录制、转换以及流传输音视频的完整跨平台解决方案。<br>既可以使用它的API对音视频进行处理，也可以使用它提供的工具，如 ffmpeg, ffplay, ffprobe，来编辑音视频文件。</p>
<p>——————引用知乎：<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/142593316">1. FFmpeg基本常识及编码流程 - 知乎 (zhihu.com)</a></p>
<p>FFmpeg是一套可以用来记录、转换数字音频、视频，并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频&#x2F;视频编解码库libavcodec，为了保证高可移植性和编解码质量，libavcodec里很多code都是从头开发的。</p>
<p>——————引用知乎：<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/89872960">30分带你从认识FFmpeg到玩转FFmpeg - 知乎 (zhihu.com)</a></p>
</blockquote>
<h3 id="为什么要使用Ffmpeg"><a href="#为什么要使用Ffmpeg" class="headerlink" title="为什么要使用Ffmpeg"></a>为什么要使用Ffmpeg</h3><blockquote>
<p>Ffmpeg是一个功能十分强大的视频编解码库（音视频编码解读：引用知乎：[音视频编解码基础 - 知乎 (zhihu.com)](<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/28058109#:~:text=%E9%9F%B3%E8%A7%86%E9%A2%91%E7%BC%96%E8%A7%A3%E7%A0%81%E5%9F%BA%E7%A1%80">https://zhuanlan.zhihu.com/p/28058109#:~:text=音视频编解码基础</a> 1 流媒体技术的基本过程 总体上说，视频从产生到传递到观看者之间的过程主要分为这么几个阶段：录制—编码—传输—解码—播放 录制：即视频的制作者利用各种摄像设备，将现实中的一些连续的场景片段记录下来。 编码：对录制好的视频进行格式化处理，以方便在网络上传输。 主要有对视频和音频分别进行压缩编码、将音视频进行打包封装两个步骤。 …,和视频一样，音频编码也是为了将音频原始数据转换为音频码流，以便在网络传输。 常见的有： … 6 流媒体协议 RTP RTCP )），我们以下只使用它的其中一个功能，就是将音视频以某种编码格式编码后再切割成M3U8格式。</p>
<p>因为传统视频都是Mp4、MKV、Avi等格式，视频不长时还好说；视频很长时，浏览器或App从网络上加载视频时，需要将一部完整的视频加载下来后，视频才能播放，这无疑是十分耗费网络资源，又浪费时间；为什么不能边看边加载呢，或者我需要看哪一段就加载哪一段，于是M3U8流媒体格式便出来了。</p>
<p>M3U8：其实是一系列视频文件的索引，通过该索引文件，查找需要播放的某一段视频。</p>
<p>HLS：是具体的视频格式，比如将一个十分钟的视频按每10秒分割一段，那么将产生60段长度很短的视频，这种视频的格式就是HLS格式，为什么不用Mp4呢，因为Mp4在两段视频加载间隔会发生画面撕裂，影响观感。而储存这些HLS视频的索引文件就叫M3U8文件。</p>
</blockquote>
<h3 id="Java使用Ffmpeg"><a href="#Java使用Ffmpeg" class="headerlink" title="Java使用Ffmpeg"></a>Java使用Ffmpeg</h3><blockquote>
<p>因为Ffmpeg是用C++编写的一个命令行软件，在没有官方提供的JNI程序的情况下，Java是无法直接调用Ffmpeg的；需要使用Java操作命令行的类以操作命令行的方式操作Ffmpeg；又由于没有对应的jar包支持，需要将网络上下载对应操作系统的Ffmpeg实现，将其封装成对应的jar包。</p>
</blockquote>
<h3 id="调用方式"><a href="#调用方式" class="headerlink" title="调用方式"></a>调用方式</h3><h4 id="导入依赖"><a href="#导入依赖" class="headerlink" title="导入依赖"></a>导入依赖</h4><figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"> <span class="comment">&lt;!-- ffmpeg视频解析包 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>ws.schild<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jave-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- ffmpeg win64版本 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>ws.schild<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jave-nativebin-win64<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- ffmpeg linux64版本 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>ws.schild<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jave-nativebin-linux64<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="执行相关代码"><a href="#执行相关代码" class="headerlink" title="执行相关代码"></a>执行相关代码</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 获取ffmpeg的绝对位置</span></span><br><span class="line"><span class="type">DefaultFFMPEGLocator</span> <span class="variable">defaultFFMPEGLocator</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DefaultFFMPEGLocator</span>();</span><br><span class="line"><span class="type">String</span> <span class="variable">ffmpegPath</span> <span class="operator">=</span> defaultFFMPEGLocator.getExecutablePath();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 命令详解：</span></span><br><span class="line"><span class="comment">     * ffmpeg -i xxx/xxx.mp4 -c:v libx264 -c:a aac -hls_time 10 -hls_list_size 0 -strict -2 -s 1920x1080 -f hls -threads 10 -preset ultrafast xxx/xxx.m3u8</span></span><br><span class="line"><span class="comment">     * 1. ffmpeg 指ffmpeg应用的路径</span></span><br><span class="line"><span class="comment">     * 2. -i 接需要编码的视频地址</span></span><br><span class="line"><span class="comment">     * 3. -c:v libx264 -c:a aac 指视频按h.254编码，音频按aac编码</span></span><br><span class="line"><span class="comment">     * 4. -hls_time 10 表示分片大小，既视频按10秒段进行切分</span></span><br><span class="line"><span class="comment">     * 5. -hls_list_size 0 -strict -2 表示视频按10秒每段切分，为了控制大小，会发生2秒左右的偏移</span></span><br><span class="line"><span class="comment">     * 6. -s 1920x1080 输出视频的分辨率</span></span><br><span class="line"><span class="comment">     * 7. -f hls 视频输出格式是hls</span></span><br><span class="line"><span class="comment">     * 8. -threads 10 -preset ultrafast 开启多线程编码，10个线程，并非越多越好，10个左右刚好合适</span></span><br><span class="line"><span class="comment">     * 9. xxx/xxx.m3u8 输出视频的位置，格式m3u8</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 执行命令</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> inVideoPath  输入视频的地址</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> outVideoPath 输出视频的地址</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="keyword">private</span> <span class="type">int</span> <span class="title function_">execute</span><span class="params">(String inVideoPath, String outVideoPath)</span> &#123;</span><br><span class="line">    log.info(<span class="string">&quot;ffmepg位置：&#123;&#125;&quot;</span>, ffmpegPath);</span><br><span class="line">    <span class="comment">// ffmpeg程序位置</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">cmd</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(ffmpegPath)</span><br><span class="line">        .append(<span class="string">&quot; -i &quot;</span>)</span><br><span class="line">        <span class="comment">// 输入视频位置</span></span><br><span class="line">        .append(inVideoPath)</span><br><span class="line">        .append(<span class="string">&quot; -c:v libx264 -c:a aac -hls_time &quot;</span>)</span><br><span class="line">        <span class="comment">// 分片大小，每个分片大小20秒</span></span><br><span class="line">        .append(<span class="number">20</span>)</span><br><span class="line">        .append(<span class="string">&quot; -hls_list_size 0 -strict -2 -s 1920x1080 -f hls -threads &quot;</span>)</span><br><span class="line">        <span class="comment">// 线程数，10个线程，10个左右最优</span></span><br><span class="line">        .append(<span class="number">10</span>)</span><br><span class="line">        .append(<span class="string">&quot; -preset ultrafast &quot;</span>)</span><br><span class="line">        <span class="comment">// 输出位置</span></span><br><span class="line">        .append(outVideoPath)</span><br><span class="line">        .toString();</span><br><span class="line"></span><br><span class="line">    <span class="type">Runtime</span> <span class="variable">runtime</span> <span class="operator">=</span> Runtime.getRuntime();</span><br><span class="line">    <span class="type">Process</span> <span class="variable">ffmpeg</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="type">InputStream</span> <span class="variable">errorIs</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        ffmpeg = runtime.exec(cmd);</span><br><span class="line">        <span class="comment">// 错误日志</span></span><br><span class="line">        errorIs = ffmpeg.getErrorStream();</span><br><span class="line">        <span class="comment">// info日志</span></span><br><span class="line">        <span class="type">OutputStream</span> <span class="variable">os</span> <span class="operator">=</span> ffmpeg.getOutputStream();</span><br><span class="line">        <span class="comment">// 在执行过程中执行y，代表统一执行</span></span><br><span class="line">        os.write(<span class="string">&quot;y&quot;</span>.getBytes(<span class="string">&quot;UTF-8&quot;</span>));</span><br><span class="line">        os.close();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// false，关闭流信息，确保ffmpeg执行完毕后关闭</span></span><br><span class="line">    <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> close(ffmpeg, errorIs, <span class="literal">false</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> res;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 命令详解：</span></span><br><span class="line"><span class="comment">     * ffmpeg -i xxx/xxx.mp4 -ss 00:00:01 -frames:v 1 xxx/xxx.png</span></span><br><span class="line"><span class="comment">     * 1. ffmpeg 指ffmpeg应用的路径</span></span><br><span class="line"><span class="comment">     * 2. -i 接需要编码的视频地址</span></span><br><span class="line"><span class="comment">     * 3. -ss 00:00:01 -frames:v 1 获取视频第一秒的第一帧为视频封面</span></span><br><span class="line"><span class="comment">     * 4. xxx/xxx.png 封面图片输出地址</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取视频封面与时长</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> inVideoPath 输入视频地址</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> outCoverPath 输出封面地址</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> duration 0:执行失败</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="keyword">private</span> <span class="type">int</span> <span class="title function_">getVideoInfo</span><span class="params">(String inVideoPath, String outCoverPath)</span> &#123;</span><br><span class="line">    <span class="type">String</span> <span class="variable">cmd</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(ffmpegPath)</span><br><span class="line">        .append(<span class="string">&quot; -i &quot;</span>)</span><br><span class="line">        <span class="comment">// 输入视频位置</span></span><br><span class="line">        .append(inVideoPath)</span><br><span class="line">        <span class="comment">// 获取视频第一帧为封面</span></span><br><span class="line">        .append(<span class="string">&quot; -ss 00:00:01 -frames:v 1 &quot;</span>)</span><br><span class="line">        .append(outCoverPath)</span><br><span class="line">        .toString();</span><br><span class="line"></span><br><span class="line">    <span class="type">Runtime</span> <span class="variable">runtime</span> <span class="operator">=</span> Runtime.getRuntime();</span><br><span class="line">    <span class="type">Process</span> <span class="variable">ffmpeg</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="type">InputStream</span> <span class="variable">errorIs</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        ffmpeg = runtime.exec(cmd);</span><br><span class="line">        <span class="comment">// 开启日志</span></span><br><span class="line">        errorIs = ffmpeg.getErrorStream();</span><br><span class="line">        <span class="comment">// 输入指令y，表示同意跳过执行</span></span><br><span class="line">        <span class="type">OutputStream</span> <span class="variable">os</span> <span class="operator">=</span> ffmpeg.getOutputStream();</span><br><span class="line">        os.write(<span class="string">&quot;y&quot;</span>.getBytes(<span class="string">&quot;UTF-8&quot;</span>));</span><br><span class="line">        os.close();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="type">int</span> <span class="variable">duration</span> <span class="operator">=</span> close(ffmpeg, errorIs, <span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> duration;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 打印过程并关闭</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> ffmpeg</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> errorIs</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> duration</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="keyword">private</span> <span class="type">int</span> <span class="title function_">close</span><span class="params">(Process ffmpeg, InputStream errorIs, <span class="type">boolean</span> printLog)</span> &#123;</span><br><span class="line">    <span class="comment">//打印过程</span></span><br><span class="line">    <span class="type">StringBuilder</span> <span class="variable">info</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>();</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">len</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (!((len = errorIs.read()) != -<span class="number">1</span>)) <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">if</span> (printLog) &#123;</span><br><span class="line">                info.append((<span class="type">char</span>) len);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 正则匹配时长信息</span></span><br><span class="line">    <span class="type">int</span> <span class="variable">duration</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (printLog) &#123;</span><br><span class="line">        <span class="type">Matcher</span> <span class="variable">matcher</span> <span class="operator">=</span> Pattern.compile(<span class="string">&quot;(?i)duration:\\s*([0-9\\:\\.]+)&quot;</span>)</span><br><span class="line">            .matcher(info.toString());</span><br><span class="line">        <span class="keyword">while</span> (matcher.find()) &#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">group</span> <span class="operator">=</span> matcher.group(<span class="number">1</span>);</span><br><span class="line">            duration = getSeconds4Str(group);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        log.info(<span class="string">&quot;时长(s)：&#123;&#125;&quot;</span>, duration);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    info = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">if</span> (errorIs != <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            errorIs.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Throwable t) &#123;</span><br><span class="line">            log.warn(<span class="string">&quot;关闭输入流失败&quot;</span>, t);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 确保命令执行完毕</span></span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        ffmpeg.waitFor();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (InterruptedException ex) &#123;</span><br><span class="line">        log.error(<span class="string">&quot;在等待过程中强制关闭：&#123;&#125;&quot;</span>, ex);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> ffmpeg.exitValue();</span><br><span class="line">    <span class="keyword">if</span> (res != <span class="number">0</span>) &#123;</span><br><span class="line">        duration = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ffmpeg != <span class="literal">null</span>) &#123;</span><br><span class="line">        ffmpeg.destroy();</span><br><span class="line">        ffmpeg = <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> duration;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 字符时间格式化为秒</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> durationStr</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="keyword">private</span> <span class="type">int</span> <span class="title function_">getSeconds4Str</span><span class="params">(String durationStr)</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">duration</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (<span class="literal">null</span> != durationStr &amp;&amp; durationStr.length() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        String[] durationStrArr = durationStr.split(<span class="string">&quot;\\:&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">hour</span> <span class="operator">=</span> durationStrArr[<span class="number">0</span>];</span><br><span class="line">        <span class="type">String</span> <span class="variable">minute</span> <span class="operator">=</span> durationStrArr[<span class="number">1</span>];</span><br><span class="line">        <span class="comment">//特殊</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">second</span> <span class="operator">=</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="type">String</span> <span class="variable">secondTmp</span> <span class="operator">=</span> durationStrArr[<span class="number">2</span>];</span><br><span class="line">        <span class="keyword">if</span> (secondTmp.contains(<span class="string">&quot;.&quot;</span>)) &#123;</span><br><span class="line">            String[] seconedTmpArr = secondTmp.split(<span class="string">&quot;\\.&quot;</span>);</span><br><span class="line">            second = seconedTmpArr[<span class="number">0</span>];</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            second = secondTmp;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            duration = Integer.parseInt(hour) * <span class="number">3600</span> + Integer.parseInt(minute) * <span class="number">60</span> + Integer.parseInt(second);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> duration;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="写在最后"><a href="#写在最后" class="headerlink" title="写在最后"></a>写在最后</h3><ul>
<li>以上只是表示Java集成Ffmpeg将视频转成m3u8</li>
<li>其实SpringBoot集成Ffmpeg也是跟以上差不多，只是将代码执行部分放入Spring容器，将部分配置写入配置文件</li>
<li>像视频转码切分这种十分消耗系统资源与时间的操作，将操作放入线程池中</li>
<li>以下简略说明下Web应用从上传视频到将剪切好的视频上传到OSS的具体过程<ul>
<li>前端上传的视频放入后端的某个临时目录</li>
<li>上传完毕后调用ffmpeg视频切割方法将视频转成m3u8，转好后放入后端的另一个临时目录（该过程丢到线程池中执行）</li>
<li>删除存储原版视频的目录，将存储m3u8视频目录中的所有.m3u8和.hls文件上传到OSS（该过程也可以丢到线程池中执行）</li>
<li>在上传过程中提前构造好视频目录地址，存入数据库</li>
<li>上传完毕，提示视频转码完毕</li>
</ul>
</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://tengfei2233.gitee.io">Fei</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://tengfei2233.gitee.io/2023/3/2/Java%E8%B0%83%E7%94%A8Ffmpeg%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E8%BD%ACM3U8/">https://tengfei2233.gitee.io/2023/3/2/Java%E8%B0%83%E7%94%A8Ffmpeg%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E8%BD%ACM3U8/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://tengfei2233.gitee.io" target="_blank">飞の博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Java/">Java</a><a class="post-meta__tags" href="/tags/Ffmpeg/">Ffmpeg</a><a class="post-meta__tags" href="/tags/%E8%A7%86%E9%A2%91%E8%BD%AC%E7%A0%81/">视频转码</a><a class="post-meta__tags" href="/tags/M3U8/">M3U8</a></div><div class="post_share"><div class="social-share" data-image="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-3-2/ffmpeg.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.staticfile.org/butterfly-extsrc/1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.staticfile.org/butterfly-extsrc/1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/wechat.png" target="_blank"><img class="post-qr-code-img" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/wechat.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/alipay.png" target="_blank"><img class="post-qr-code-img" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/alipay.png" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/2/27/SpringBoot%E9%9B%86%E6%88%90%E6%94%AF%E4%BB%98%E5%AE%9D%E6%B2%99%E7%AE%B1%E6%94%AF%E4%BB%98/" title="SpringBoot集成支付宝沙箱支付"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-3-2/alipay.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">SpringBoot集成支付宝沙箱支付</div></div></a></div><div class="next-post pull-right"><a href="/2023/3/14/%E6%9E%84%E5%BB%BA%E5%8F%AF%E9%87%8D%E5%A4%8D%E8%AF%BB%E5%8F%96Request-Response/" title="构建可重复读取Request/Response"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-3-14/request.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">构建可重复读取Request/Response</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/11/21/netty%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%EF%BC%8C%E5%9F%BA%E6%9C%AC%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90%EF%BC%8C%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3-%E6%9B%B4%E6%96%B0%E4%B8%AD/" title="netty的基本使用，基本原理分析，相关问题解(更新中...)"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-11-10/netty-ai.png" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2023-11-21</div><div class="title">netty的基本使用，基本原理分析，相关问题解(更新中...)</div></div></a></div><div><a href="/2023/6/25/Java%E5%8E%9F%E7%94%9FNio%E6%9C%8D%E5%8A%A1%E7%AB%AF%E4%B8%8E%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%BC%96%E5%86%99/" title="Java原生Nio服务端与客户端编写"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-25/Nio.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2023-06-25</div><div class="title">Java原生Nio服务端与客户端编写</div></div></a></div><div><a href="/2023/6/15/SpringBoot%E9%9B%86%E6%88%90SpringSecurity%E5%AE%9E%E7%8E%B0%E5%89%8D%E5%90%8E%E7%AB%AF%E5%88%86%E7%A6%BB%E6%9D%83%E9%99%90%E6%A0%A1%E9%AA%8C-%E6%96%B0%E7%89%88%E9%85%8D%E7%BD%AE%E6%96%B9%E5%BC%8F/" title="SpringBoot集成SpringSecurity实现前后端分离权限校验(新版配置方式)(待更新...)"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-15/security.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2023-07-17</div><div class="title">SpringBoot集成SpringSecurity实现前后端分离权限校验(新版配置方式)(待更新...)</div></div></a></div><div><a href="/2023/6/15/Jackson%E8%A7%A3%E5%86%B3SpringMVC%E5%85%A8%E5%B1%80%E5%BA%8F%E5%88%97%E5%8C%96%E8%BF%94%E5%9B%9E%E4%B8%ADLong%E7%B1%BB%E5%9E%8B%E7%B2%BE%E5%BA%A6%E4%B8%A2%E5%A4%B1%E9%97%AE%E9%A2%98/" title="配置SpringMvc全局序列化与反序列化，解决Long类型下精度丢失问题（使用FastJson2）"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-15/js.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2023-06-19</div><div class="title">配置SpringMvc全局序列化与反序列化，解决Long类型下精度丢失问题（使用FastJson2）</div></div></a></div><div><a href="/2023/3/14/%E6%9E%84%E5%BB%BA%E5%8F%AF%E9%87%8D%E5%A4%8D%E8%AF%BB%E5%8F%96Request-Response/" title="构建可重复读取Request&#x2F;Response"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-3-14/request.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2023-03-14</div><div class="title">构建可重复读取Request&#x2F;Response</div></div></a></div><div><a href="/2023/2/27/SpringBoot%E9%9B%86%E6%88%90%E6%94%AF%E4%BB%98%E5%AE%9D%E6%B2%99%E7%AE%B1%E6%94%AF%E4%BB%98/" title="SpringBoot集成支付宝沙箱支付"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-3-2/alipay.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2023-02-27</div><div class="title">SpringBoot集成支付宝沙箱支付</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/fei.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Fei</div><div class="author-info__description">当你想起来了，那就记录一点吧</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">24</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/tengfei2233"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/tengfei2233" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://gitee.com/tengfei2233" target="_blank" title="Gitee"><i class="fab fa-git"></i></a><a class="social-icon" href="/youfeige@outlook.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">当你迷茫的时候，那就静下心想想吧！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Java%E9%9B%86%E6%88%90Ffmpeg%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E8%BD%ACM3U8"><span class="toc-number">1.</span> <span class="toc-text">Java集成Ffmpeg实现视频转M3U8</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Ffmpeg%E6%98%AF%E4%BB%80%E4%B9%88"><span class="toc-number">1.1.</span> <span class="toc-text">Ffmpeg是什么</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E4%BD%BF%E7%94%A8Ffmpeg"><span class="toc-number">1.2.</span> <span class="toc-text">为什么要使用Ffmpeg</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Java%E4%BD%BF%E7%94%A8Ffmpeg"><span class="toc-number">1.3.</span> <span class="toc-text">Java使用Ffmpeg</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B0%83%E7%94%A8%E6%96%B9%E5%BC%8F"><span class="toc-number">1.4.</span> <span class="toc-text">调用方式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AF%BC%E5%85%A5%E4%BE%9D%E8%B5%96"><span class="toc-number">1.4.1.</span> <span class="toc-text">导入依赖</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%89%A7%E8%A1%8C%E7%9B%B8%E5%85%B3%E4%BB%A3%E7%A0%81"><span class="toc-number">1.4.2.</span> <span class="toc-text">执行相关代码</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%99%E5%9C%A8%E6%9C%80%E5%90%8E"><span class="toc-number">1.5.</span> <span class="toc-text">写在最后</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/11/21/netty%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%EF%BC%8C%E5%9F%BA%E6%9C%AC%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90%EF%BC%8C%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3-%E6%9B%B4%E6%96%B0%E4%B8%AD/" title="netty的基本使用，基本原理分析，相关问题解(更新中...)"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-11-10/netty-ai.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="netty的基本使用，基本原理分析，相关问题解(更新中...)"/></a><div class="content"><a class="title" href="/2023/11/21/netty%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%EF%BC%8C%E5%9F%BA%E6%9C%AC%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90%EF%BC%8C%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3-%E6%9B%B4%E6%96%B0%E4%B8%AD/" title="netty的基本使用，基本原理分析，相关问题解(更新中...)">netty的基本使用，基本原理分析，相关问题解(更新中...)</a><time datetime="2023-11-21T09:27:51.000Z" title="发表于 2023-11-21 17:27:51">2023-11-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/11/10/%E5%9C%A8conda%E7%8E%AF%E5%A2%83%E4%B8%8B%E8%BF%90%E8%A1%8Cstable-diffusion-webui-AI%E7%BB%98%E5%9B%BE/" title="在conda环境下运行stable-diffusion-webui AI绘图"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-11-10/ai.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="在conda环境下运行stable-diffusion-webui AI绘图"/></a><div class="content"><a class="title" href="/2023/11/10/%E5%9C%A8conda%E7%8E%AF%E5%A2%83%E4%B8%8B%E8%BF%90%E8%A1%8Cstable-diffusion-webui-AI%E7%BB%98%E5%9B%BE/" title="在conda环境下运行stable-diffusion-webui AI绘图">在conda环境下运行stable-diffusion-webui AI绘图</a><time datetime="2023-11-10T07:30:51.000Z" title="发表于 2023-11-10 15:30:51">2023-11-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/11/10/%E5%9C%A8Ubuntu20%E4%B8%8A%E5%AE%89%E8%A3%85CUDA-cuDNN-Miniconda%E7%8E%AF%E5%A2%83(%E5%85%B6%E5%AE%83%E7%B3%BB%E7%BB%9F%E5%90%8C%E7%90%86)/" title="在Ubuntu20上安装CUDA+cuDNN+Miniconda环境(其它系统同理)"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-11-10/ai.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="在Ubuntu20上安装CUDA+cuDNN+Miniconda环境(其它系统同理)"/></a><div class="content"><a class="title" href="/2023/11/10/%E5%9C%A8Ubuntu20%E4%B8%8A%E5%AE%89%E8%A3%85CUDA-cuDNN-Miniconda%E7%8E%AF%E5%A2%83(%E5%85%B6%E5%AE%83%E7%B3%BB%E7%BB%9F%E5%90%8C%E7%90%86)/" title="在Ubuntu20上安装CUDA+cuDNN+Miniconda环境(其它系统同理)">在Ubuntu20上安装CUDA+cuDNN+Miniconda环境(其它系统同理)</a><time datetime="2023-11-10T07:27:51.000Z" title="发表于 2023-11-10 15:27:51">2023-11-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/8/27/Docker-Compose%E5%AD%A6%E4%B9%A0/" title="Docker Compose学习"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Docker Compose学习"/></a><div class="content"><a class="title" href="/2023/8/27/Docker-Compose%E5%AD%A6%E4%B9%A0/" title="Docker Compose学习">Docker Compose学习</a><time datetime="2023-08-27T15:34:50.000Z" title="发表于 2023-08-27 23:34:50">2023-08-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/8/27/Dockerfile%E5%AD%A6%E4%B9%A0/" title="Dockerfile学习"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Dockerfile学习"/></a><div class="content"><a class="title" href="/2023/8/27/Dockerfile%E5%AD%A6%E4%B9%A0/" title="Dockerfile学习">Dockerfile学习</a><time datetime="2023-08-27T15:34:37.000Z" title="发表于 2023-08-27 23:34:37">2023-08-27</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-3-2/ffmpeg.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2024 By Fei</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">欢迎进入<a href="tengfei2233.gitee.io/">飞の博客</a>!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="https://cdn.staticfile.org/hexo-theme-butterfly/4.7.0/js/utils.min.js"></script><script src="https://cdn.staticfile.org/hexo-theme-butterfly/4.7.0/js/main.min.js"></script><script src="https://cdn.staticfile.org/hexo-theme-butterfly/4.7.0/js/tw_cn.min.js"></script><script src="https://cdn.staticfile.org/fancyapps-ui/4.0.31/fancybox.umd.min.js"></script><script src="https://cdn.staticfile.org/instant.page/5.1.1/instantpage.min.js" type="module"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.staticfile.org/pangu/4.0.7/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: '0kbeJfFlvhvkYfqMqwwVCx1a-gzGzoHsz',
      appKey: 'rziWUw1MTHMW7d9eeCQzu9HV',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.staticfile.org/valine/1.5.1/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script defer="defer" id="ribbon" src="https://cdn.staticfile.org/butterfly-extsrc/1.1.3/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div><script src="https://cdn.staticfile.org/hexo-theme-butterfly/4.7.0/js/search/local-search.min.js"></script></div></body></html>